package com.ljb.utilslibrary.commUtils;

/**
 * 单例的工具类,只需继承此类,重写newInstance()方法即可
 * Created by ljb on 2017/6/29.
 */

public abstract class SingletonUtils<T> {
    //单例对象
    private T instance;

    /**
     * 生成单例对象
     *
     * @return 单例对象
     */
    protected abstract T newInstance();

    /**
     * 获取单例对象
     *
     * @return 泛型为T的单例对象
     */
    public final T getInstance() {
        if (instance == null) {
            synchronized (SingletonUtils.class) {
                if (instance == null) {
                    instance = newInstance();
                }
            }
        }
        return instance;
    }
}
